package cn.com.gridinfo.sooc.core.login.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.PublicConst;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;
import cn.com.gridinfo.sooc.utils.MD5;

/**
 * 注册的dao
 *
 * @author yingchunli
 */
public class RegsiterDao extends SoocIDao {

    private static String serverName;//节点名称
    private static String serverUrl;//节点地址
    public static String userzh;//用户账号
    public static String userPwd;//用户密码

    public static String getUserzh() {
        return userzh;
    }

    public static String getUserPwd() {
        return userPwd;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getServerName() {

        return serverName;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    public String status;//确认是否成功的信息，  "0": 成功  非0：不成功
    public String statusCode;//错误信息
    public List<NodeServerInfo> schoolInfoList = new ArrayList<>();//学校列表信息

    public RegsiterDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
//            status=0成功
        if (result != null) {
            status = result.get("status").asText();
            statusCode = result.get("statusCode").asText();
            if (!status.equals("0")) {
                return;
            }
        } else {
            return;
        }
        Log.d("result:" + result.toString());
        switch (requestCode) {
            case CodeConst.GET_SCHOOLLIST:
                //获取学校列表
                schoolInfoList = JsonUtil.node2pojoList(result.get("data"), NodeServerInfo.class);
                break;
            case CodeConst.GET_YANZHENGMA:
                //获取验证码
                break;
            case CodeConst.REGSITER:
                //注册
                break;
            case CodeConst.JUDGE:
                //验证身份证
                break;
            case CodeConst.MATCH_INFO:
                //验证身份证和姓名是否匹配
                break;
        }
    }


    /**
     * 注册时，获取学校列表
     *
     * @param sqmc        搜索名称(模糊)
     * @param requestCode 请求 major
     */
    public void getSchoolList(String sqmc, int requestCode) {
        String url = PublicConst.CLOUD_URL + "/index.php?m=api&c=User&a=getRegList";
        Log.d("url->" + url);
        Map<String, String> map = new HashMap<>();
        //名称不为空
        if (!StringUtils.isNull(sqmc)) {
            map.put("sqmc", sqmc);
        }
        getRequestForCode(url, map, requestCode, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 获取验证码
     *
     * @param mobile 手机号码
     * @param type   短信类型:  1:密码找回  2:注册验证
     * @param dlxtbb 登录系统版本
     * @param dlsbxh 登录设备型号
     * @param yjbsm  硬件标识码
     */
    public void getYanzhengma(String mobile, String type, String dlxtbb, String dlsbxh, String yjbsm, int requestCode) {
        String url = HttpUrl.getHttpUrl(serverUrl, UrlConst.Module.User.toString(), "sms");
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        params.add("mobile", mobile);
        params.add("type", type);
        params.add("dlxtbb", dlxtbb);
        params.add("dlsbxh", dlsbxh);
        params.add("yjbsm", yjbsm);
        postRequest(url, params, requestCode, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 用户注册
     *
     * @param identitytype 注册身份，2=老师，3=学生，5=游客
     * @param password     用户密码
     * @param cellphone    用户手机号
     * @param mobile_code  验证码
     * @param username     用户昵称
     * @param xm           用户真实姓名
     * @param sfzjh        用户身份证号
     * @param invite       邀请码
     * @param imei         手机imei编码
     */
    public void regsiter(int identitytype, String cellphone, String password, String mobile_code,
                         String username, String xm, String sfzjh, String invite, String imei, int requestCode) {
        userzh = cellphone;
        userPwd = password;
        String url = HttpUrl.getHttpUrl(serverUrl, UrlConst.Module.User.toString(), "register");

        RequestParams params = new RequestParams();
        params.add("identitytype", identitytype + "");
        params.add("email", cellphone + "@" + cellphone.substring(0, 3) + ".com");
        params.add("cellphone", cellphone);
        params.add("password", MD5.encode(StringUtils.changeSpecialChar(password)));
        params.add("mobile_code", mobile_code);
        if (identitytype != 5) {
            //老师，学生
            params.add("xm", xm);
            params.add("sfzjh", sfzjh);
        } else {
            //游客
            params.add("username", username);
        }
        params.add("invite", invite);
        params.add("imei", imei);
        Log.d("注册参数-》" + params.toString());
        postRequest(url, params, requestCode, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 验证身份证
     *
     * @param type        注册身份，2=老师，3=学生  5=体验
     * @param key         email：邮箱验证      sfzjh：身份证验证
     * @param value       项目的值
     * @param requestCode 请求码
     */
    public void judge(int type, String key, String value, int requestCode) {
        String url = HttpUrl.getHttpUrl(serverUrl, UrlConst.Module.User.toString(), "checkMember");
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        params.add("type[]", key);
        params.add("key[]", value);
        if (type != 5) {
            params.add("type[]", "identitytype");
            params.add("key[]", type + "");
        }
        postRequest(url, params, requestCode, PublicConst.NOT_NEED_TOKEN);
    }

    /**
     * 验证身份证号和姓名
     *
     * @param keyValues
     * @param requestCode
     */
    public void judgeInfo(Map<String, String> keyValues, int requestCode) {
        String url = HttpUrl.getHttpUrl(serverUrl, UrlConst.Module.User.toString(), "checkMember");
        Log.d("url->" + url);
        RequestParams params = new RequestParams();
        int i = 0;
        for (String key : keyValues.keySet()) {
            params.add("type[" + i + "]", key);
            params.add("key[" + i + "]", keyValues.get(key));
            i++;
        }
        Log.d("params=" + params.toString());
        postRequest(url, params, requestCode, PublicConst.NOT_NEED_TOKEN);
    }

    //返回学校列表信息
    public List<NodeServerInfo> getSchoolInfoList() {
        return schoolInfoList;
    }

    //获取网络请求是否成功的信息  status="0"成功  "1" 失败
    public String getStatus() {
        return status;
    }

    //获取网络请求错误的内容信息
    public String getStatusCode() {
        return statusCode;
    }
}
